package com.mallify.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.mallify.common.model.express.Express;
import com.mallify.common.page.CommonPage;
import com.mallify.common.request.ExpressSearchRequest;
import com.mallify.common.request.ExpressUpdateRequest;
import com.mallify.common.request.ExpressUpdateShowRequest;
import com.mallify.common.request.PageParamRequest;
import com.mallify.common.response.Result;
import com.mallify.service.service.ExpressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 管理端登录服务
 * +----------------------------------------------------------------------
 * | HANSON [ HANSON赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Licensed HANSON并不是自由软件，未经许可不能去掉HANSON相关版权
 * +----------------------------------------------------------------------
 * | Author: HANSON Team <hanson0622@126.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin/express")
@Api(tags = "设置 -- 物流 -- 公司")
public class ExpressController {

    @Resource
    private ExpressService expressService;

    /**
     * 分页显示快递公司表
     *
     * @param request          ExpressSearchRequest 搜索条件
     * @param pageParamRequest 分页参数
     */
    @PreAuthorize("hasAnyAuthority('admin:express:list')")
    @ApiOperation(value = "分页显示快递公司表")
    @GetMapping("/list")
    @ApiImplicitParam(name = "keywords", value = "搜索关键字")
    public Result<CommonPage<Express>> getList(@Validated ExpressSearchRequest request,
                                               @ModelAttribute PageParamRequest pageParamRequest) {
        CommonPage<Express> expressCommonPage = CommonPage.restPage(expressService.getList(request, pageParamRequest));
        return Result.success(expressCommonPage);
    }

    /**
     * 编辑快递公司
     */
    @PreAuthorize("hasAnyAuthority('admin:express:update')")
    @ApiOperation(value = "编辑快递公司")
    @PostMapping("/update")
    public Result<String> update(@RequestBody ExpressUpdateRequest expressRequest) {
        if (Boolean.TRUE.equals(expressService.updateExpress(expressRequest))) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 修改显示状态
     */
    @PreAuthorize("hasAnyAuthority('admin:express:update:show')")
    @ApiOperation(value = "修改显示状态")
    @PostMapping("/update/show")
    public Result<String> update(@RequestBody @Validated ExpressUpdateShowRequest expressRequest) {
        if (Boolean.TRUE.equals(expressService.updateExpressShow(expressRequest))) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 同步物流公司
     */
    @PreAuthorize("hasAnyAuthority('admin:express:sync')")
    @ApiOperation(value = "同步物流公司")
    @GetMapping("/sync/express")
    public Result<String> syncExpress() {
        if (Boolean.TRUE.equals(expressService.syncExpress())) {
            return Result.success();
        }
        return Result.failed();
    }

    /**
     * 查询快递公司表信息
     *
     * @param id Integer
     */
    @PreAuthorize("hasAnyAuthority('admin:express:info')")
    @ApiOperation(value = "详情")
    @GetMapping(value = "/info")
    @ApiImplicitParam(name = "id", value = "快递公司ID", required = true)
    public Result<Express> info(@RequestParam Integer id) {
        return Result.success(expressService.getInfo(id));
    }

    /**
     * 查询全部物流公司
     */
    @PreAuthorize("hasAnyAuthority('admin:express:all')")
    @ApiOperation(value = "查询全部物流公司")
    @GetMapping(value = "/listAll")
    public Result<List<Express>> all(@RequestParam(value = "type") String type) {
        return Result.success(expressService.findAll(type));
    }

    /**
     * 查询物流公司面单模板
     */
    @PreAuthorize("hasAnyAuthority('admin:express:template')")
    @ApiOperation(value = "查询物流公司面单模板")
    @GetMapping(value = "/template")
    @ApiImplicitParam(name="com", value="快递公司编号", required = true)
    public Result<JSONObject> template(@RequestParam(value = "com") String com) {
        return Result.success(expressService.template(com));
    }
}